package com.leo;

import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;

/**
 * @ClassName MyScheduleService
 * @Description
 * @Author wangss
 * @date 2020.05.26 14:21
 * @Version 1.0
 */
public class MyScheduleService extends ScheduledService<Double> {

    private double i = 0;

    private Object progress;

    public MyScheduleService(Object progressBar) {
        this.progress = progressBar;
    }

    @Override
    protected Task<Double> createTask() {
        return new Task<Double>() {
            @Override
            protected Double call() throws Exception {
                i = i + 0.1;
                return i;
            }

            @Override
            protected void updateValue(Double value) {
                if (progress instanceof ProgressBar) {
                    ((ProgressBar) progress).setProgress(value);
                } else if (progress instanceof ProgressIndicator) {
                    ((ProgressIndicator) progress).setProgress(value);
                }
                if (value >= 1) {
                    MyScheduleService.this.cancel();
                    System.out.println("停止 = " + Thread.currentThread().getName());
                }
            }
        };
    }
}
